import Component from './Component.js';
// import PIXI from './PIXI.js';
let app = window.app;
export default class Note extends Component{
  
	constructor({beatTime, trackIndex, width, height, beatHeight, onMiss, index, trackNum}) { 

		super();

    this.speed = 1000;
    this.startY = -100;

    this.width = width;
    this.height = height;
    this.vs = width / 375;
    this.beatHeight = beatHeight;
    this.onMiss = onMiss;
    this.index = index;

    this.trackIndex = trackIndex; //Math.round(Math.random() * 3);
    const trackWidth = width/trackNum;
    const x = trackWidth * this.trackIndex + trackWidth/2;

		const sprite = new PIXI.Sprite(Math.random()>0.5?window.resources["note1"].texture:window.resources["note2"].texture);
		sprite.width = 66 * this.vs;
    sprite.height = 68 * this.vs;
    sprite.x = x - sprite.width/2;
    sprite.y = this.startY - sprite.height/2;
    sprite.visible = false;
    sprite.interactive = true;

		this.sprite = sprite;

		
		this.beatTime = beatTime; //节奏时间
		this.startTime = null; //动画开始时间
		this.caught = false; //是否被点中
		//this.touch = 0; //到达地平线时的比例
		this.status = 'waiting'; //<'waiting','moving','end'>

	}
	moveTo({x, y}){
		this.sprite.x = x - this.sprite.width/2;
		this.sprite.y = y - this.sprite.height/2;
  }
  end(){
  	this.sprite.visible = false;
	  this.status = 'end';
  }
  miss(){
  	//this.status = 'miss';
  	this.onMiss && this.onMiss();
  }
  easeOutSine(pos){
    return Math.sin(pos * (Math.PI/2));
  }
  pow(pos){
  	return Math.pow(pos, 2);
  }
  fly(now){
    //console.log(this.status,this.beatTime,now)
    if(this.status == 'end' || now + this.speed < this.beatTime){
      return;
    }

    if(this.startTime === null){
        this.startTime = now;
        this.status = 'moving';
        this.sprite.visible = true;
    }

    if(now > this.beatTime && this.status == 'moving' ){
    	//this.sprite.y = this.height + 1;
      //this.end();
      //this.miss();
      //return;
    }
    //退到app后台，又回来的
    if(this.startTime >= this.beatTime){
      this.end();
      //this.miss(); //myz miss加分0
      return;
    }
 
    let y = (this.beatHeight - this.startY) * this.pow((now - this.startTime)/(this.beatTime - this.startTime)) + this.startY;
    this.sprite.y = y + this.sprite.height/2;
   
      const radio = this.hitRadio(window.$target, this.sprite);
      if(radio){
        this.caught = true; 
        this.end();
        window.score.update(radio);
        return ;
      } 
   
    if(this.sprite.y > this.height && this.status != 'end'){
    	this.end();
    	//this.miss();
    	//console.log('miss: ', this.index)
    } 
  }
  hitRadio(a, b){
    let aTop = a.y + 51*this.vs;
    let aBottom = a.y + a.height-133*this.vs;
    let bTop = b.y;
    let bBottom = b.y + b.height; 
    
    let aLeft = a.x+86*this.vs;
    let aRight = a.x + a.width-86*this.vs;
    let bLeft = b.x;
    let bRight = b.x + b.width; 

    if(bBottom < aTop || bTop > aBottom){
      return 0;
    }
    if(bRight <aLeft || bLeft > aRight){
      return 0;
    }
    return (Math.min(bBottom, aBottom) - Math.max(bTop, aTop)) / b.height;
	} 
}
